package com.homevip.user.service.impl;

import com.homevip.core.page.Page;
import com.homevip.core.page.Pageable;
import com.homevip.user.dao.UserDao;
import com.homevip.user.entity.User;
import com.homevip.user.service.UserService;
import com.homevip.util.date.DateTimeUtil;
import com.homevip.util.security.MD5MixUtil;
import com.homevip.util.system.CommArray;
import com.homevip.util.system.Const;
import com.homevip.web.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户Service实现类
 *
 * @author panpan
 * @version 2014-12-18
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User getUserById(int id) {
        User user = userDao.getById(id);
        return user;
    }

    @Override
    public User getUserByAccount(String account) {
        User user = userDao.getBy("account", account);
        return user;
    }

    @Override
    @Transactional
    public ResultVo saveUser(User user) {
        ResultVo resultVo = new ResultVo(true, "编辑成功");

        user.setLastdate(DateTimeUtil.getCurDateTime());

        if (null != user.getCode() && !"".equals(user.getCode())) {
            if (isExists(user, 2)) {
                resultVo.setResult(false);
                resultVo.setMsg("业务ID已被使用。");
                return resultVo;
            }
        }

        if (0 == user.getId()) {
            // 检查用户名是否已存在
            if (isExists(user, 1)) {
                resultVo.setResult(false);
                resultVo.setMsg("账号已被使用。");
                return resultVo;
            }

            user.setCreatedate(DateTimeUtil.getCurDateTime());
            user.setLastdate(DateTimeUtil.getCurDateTime());
            user.setPassword(MD5MixUtil.md5Mix(user.getPassword()));
            userDao.save(user);
        } else {
            // 某些不变的值
            User u = getUserById(user.getId());
            user.setAccount(u.getAccount());
            user.setCreatedate(u.getCreatedate());
            user.setPassword(u.getPassword());
            user.setLogintimes(u.getLogintimes());
            user.setLastlogin(u.getLastlogin());
            user.setLastip(u.getLastip());
            userDao.update(user);
        }
        return resultVo;
    }

    @Override
    public Page<User> findUser(String keyword, Integer usertype, Integer status, Pageable pageable) {
        Page<User> page = userDao.findBy(keyword, usertype, status, pageable);
        return page;
    }

    @Transactional
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    @Transactional
    public void deleteUser(User user) {
        userDao.delete(user);
    }

    @Override
    @Transactional
    public void saveUserPass(User user) {
        user.setLastdate(DateTimeUtil.getCurDateTime());
        user.setPassword(MD5MixUtil.md5Mix(user.getPassword()));
        userDao.update(user);
    }

    @Transactional
    public void initAdmin() {
        User u = userDao.getBy("account", Const.ADMINUSER_NAME);
        if (null == u) {
            u = new User();
            u.setAccount(Const.ADMINUSER_NAME);
            u.setCname(Const.ADMINUSER_NAME);
            u.setPassword(MD5MixUtil.md5Mix(Const.ADMINUSER_PWD));
            u.setUsertype(CommArray.EnumStaffType._6.getCode());
            u.setStatus(2);
            userDao.save(u);
            u.setCode("" + u.getId());
            userDao.update(u);
        }
    }

    /**
     * 判断用户是否存在
     */
    boolean isExists(User e, int flag) {
        User o = null;
        if (1 == flag) {
            o = userDao.getBy("account", e.getAccount());
        } else {
            o = userDao.getBy("code", e.getCode());
        }
        if (null != o) {
            if (0 == e.getId()) {
                return true;
            } else {
                if (e.getId() != o.getId()) {
                    return true;
                }
            }
        }
        return false;
    }
}
